const currency=[
	{
		'name':'HKD',//港币
		'rate':0.13,
		'nickname':'港币',
	},
	{
		'name':'USD',//美元
		'rate':1,
		'nickname':'美元',
	},
	{
		'name':'RMB',//人民币
		'rate':0.15,
		'nickname':'人民币',
	},
	{
		'name':'EUD',//欧元
		'rate':1.16,
		'nickname':'欧元',
	},
	{
		'name':'GBP', //英镑
		'rate':1.28,
		'nickname':'英镑',
	},
	{
		'name':'AUD',//澳元
		'rate':0.73,
		'nickname':'澳元',
	},
	{
		'name':'JPY',//日元
		'rate':0.009,
		'nickname':'日元',
	},
	{
		'name':'TWD',//台币
		'rate':0.03,
		'nickname':'台币',
	}
];
//金额数字格式
export const NumberForat=function(number, decimals, dec_point, thousands_sep,roundtag){
	 /*
    * 参数说明：
    * number：要格式化的数字
    * decimals：保留几位小数
    * dec_point：小数点符号
    * thousands_sep：千分位符号
    * roundtag:舍入参数，默认 "ceil" 向上取,"floor"向下取,"round" 四舍五入
    * */
    number = (number + '').replace(/[^0-9+-Ee.]/g, '');
    roundtag = roundtag || "ceil"; //"ceil","floor","round"
    var n = !isFinite(+number) ? 0 : +number,
        prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
        sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
        dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
        s = '',
        toFixedFix = function (n, prec) {
 
            var k = Math.pow(10, prec);
     
 
            return '' + parseFloat(Math[roundtag](parseFloat((n * k).toFixed(prec*2))).toFixed(prec*2)) / k;
        };
    s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
    var re = /(-?\d+)(\d{3})/;
    while (re.test(s[0])) {
        s[0] = s[0].replace(re, "$1" + sep + "$2");
    }
 
    if ((s[1] || '').length < prec) {
        s[1] = s[1] || '';
        s[1] += new Array(prec - s[1].length + 1).join('0');
    }
    return s.join(dec);
}

//获取年月日时分秒
export const getTime=function(val){
	let d = new Date(val);
	let times=d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate() + ' ' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds(); 
	return times;
}
//获取年月日
export const getTimeDay=function(val){
	let d = new Date(val);
	let times=d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate() ; 
	return times;
}
//判断 如果有 美元则返回美元，没有则返回第一个
export const getSlugUSD=function(data,slug){
	for (let i = 0; i < data.length; i++) {
		if(data[i].slug == slug){
			return data[i];
		}
	}
	return data[0];
}

//获取最小的年数
export const getMinYear=function(Year){
	return Math.min.apply(null, Year);
}

// 计算年龄
export const getAges=function(str){
	let   r = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);    
	if(r==null)return   false;  
	let   d= new Date(r[1], r[3]-1,r[4]);     
	if(d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]){
		let   Y= new Date().getFullYear();  
		return (Y-r[1])
	}  
}
export const currencyConverter=function(val){
	let rate = 1;
	for (let i=0;i<currency.length;i++) {
		if(val == currency[i].name){
			rate = currency[i].rate
		}
	}
	return rate;
}
export const getCurrencyName = function(val){
	let name = '美元'
	for (let i=0;i<currency.length;i++) {
		if(val == currency[i].name){
			name = currency[i].nickname;
		}
	}
	return name;
}
export const isInArray=function(arr,value){
	for(var i = 0; i < arr.length; i++){
        if(value === arr[i]){
            return true;
        }
    }
    return false;
}
//设置cookie
export const setCookie = function(setName,value,expire){
	let date=new Date()
	date.setSeconds(date.getSeconds()+expire)
	document.cookie=setName+ "="+escape(value)+"; expires="+date.toGMTString()
}
//获取cookie
export const getCookie = function(getName){
	if (document.cookie.length>0){
		let c_start=document.cookie.indexOf(getName+"=")
		if (c_start!=-1){ 
			c_start=c_start + getName.length+1 
			let c_end=document.cookie.indexOf(";",c_start)
			if (c_end==-1) c_end=document.cookie.length
				return unescape(document.cookie.substring(c_start,c_end))
			} 
		}
	return false;
}
//清除cookie
export const clearCookie = function(name){
	setCookie(name, "", -1);  
}
//组装生成家庭保障方案的api
export const creatApi=function(data){
	let temp = JSON.parse(data)
	let query ='';
	for(let item in temp){
		
		let str = '{clientName:"'+temp[item].clientName+'",gender:"'+temp[item].gender+'",age:'+temp[item].age+',isSmoker:'+temp[item].isSmoker+',adviceDetailList: [';
		let tem_pr=temp[item].adviceDetailList
		for(let k=0;k< tem_pr.length;k++){
			str += '{productId:'+tem_pr[k].productId+',currency:"'+tem_pr[k].currency+'",premium:'+tem_pr[k].premium+',totalCoverage:'+tem_pr[k].totalCoverage+',year:'+tem_pr[k].year+'}';
		}
		str += ']}';
		query+=str
	}
	return '['+query+']';
}

//整理提交家庭保障方案的数据
export const arranGement=function(data){
	let array = new Array();
	let result = {
		success:false,
		message:'',
		data:[]
	}
	for (let i = 0; i < data.length; i++) {
		let num = i+1;
		if(data[i].clientName== ''){
			result.message = '受保人'+num+':姓名未填写!';
			return result;
		}
		if(!data[i].adviceDetailList.length){
			result.message = '受保人'+num+':未选择产品!';
			return result;
		}
		let tempProudct = [];
		for (let j = 0; j < data[i].adviceDetailList.length; j++) {
			if(data[i].adviceDetailList[j].currency==''||data[i].adviceDetailList[j].year==''||data[i].adviceDetailList[j].premium==''&&data[i].adviceDetailList[j].totalCoverage==''){
				result.message = '受保人'+num+',选择的'+data[i].adviceDetailList[j].title+'产品:信息未填写完整!';
				return result;
			}
			if(data[i].adviceDetailList[j].currency!=''&&data[i].adviceDetailList[j].year!=''&&data[i].adviceDetailList[j].totalCoverage!=''&&data[i].adviceDetailList[j].premium!=''){										
				tempProudct.push({
					productId:data[i].adviceDetailList[j].productId,
					currency:data.adviceDetailList[j].currency,
					year:data[i].adviceDetailList[j].year,
					premium:data[i].adviceDetailList[j].premium,
					totalCoverage:0
				})
			}
			if(data[i].adviceDetailList[j].currency!=''&&data[i].adviceDetailList[j].year!=''&&data[i].adviceDetailList[j].totalCoverage!=''&&data[i].adviceDetailList[j].premium==''){
				tempProudct.push({
					productId:data[i].adviceDetailList[j].productId,
					currency:data[i].adviceDetailList[j].currency,
					year:data[i].adviceDetailList[j].year,
					premium:0,
					totalCoverage:data[i].adviceDetailList[j].totalCoverage
				})
			}
			if(data[i].adviceDetailList[j].currency!=''&&data[i].adviceDetailList[j].year!=''&&data[i].adviceDetailList[j].totalCoverage==''&&data[i].adviceDetailList[j].premium!=''){
				tempProudct.push({
					productId:data[i].adviceDetailList[j].productId,
					currency:data[i].adviceDetailList[j].currency,
					year:data[i].adviceDetailList[j].year,
					premium:data[i].adviceDetailList[j].premium,
					totalCoverage:0
				})
			}	
		}
		array.push({
			clientName:data[i].clientName,
			gender:data[i].gender,
			age:data[i].age,
			isSmoker:data[i].isSmoker,
			adviceDetailList:tempProudct
		})
		
	}
	result.success = true;
	result.data = array;
	return result;
}
